package com.fbs.wowo.finance.mapper;

import com.fbs.wowo.finance.entity.Bill;
import com.fbs.wowo.finance.vo.BillQuery;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class BillSqlProvider {

    public String insertSelective(Bill record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("bill");
        
        if (record.getId() != null) {
            sql.VALUES("id", "#{id,jdbcType=VARCHAR}");
        }
        
        if (record.getUserId() != null) {
            sql.VALUES("user_id", "#{userId,jdbcType=VARCHAR}");
        }
        
        if (record.getTradeId() != null) {
            sql.VALUES("trade_id", "#{tradeId,jdbcType=VARCHAR}");
        }
        
        if (record.getTradeTime() != null) {
            sql.VALUES("trade_time", "#{tradeTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getTotalFee() != null) {
            sql.VALUES("total_fee", "#{totalFee,jdbcType=DECIMAL}");
        }
        
        if (record.getIsIncome() != null) {
            sql.VALUES("is_income", "#{isIncome,jdbcType=TINYINT}");
        }
        
        if (record.getOtherSide() != null) {
            sql.VALUES("other_side", "#{otherSide,jdbcType=VARCHAR}");
        }
        
        if (record.getRefund() != null) {
            sql.VALUES("refund", "#{refund,jdbcType=VARCHAR}");
        }
        
        if (record.getTradeType() != null) {
            sql.VALUES("trade_type", "#{tradeType,jdbcType=TINYINT}");
        }
        
        if (record.getProductId() != null) {
            sql.VALUES("product_id", "#{productId,jdbcType=VARCHAR}");
        }
        
        if (record.getProductCategory() != null) {
            sql.VALUES("product_category", "#{productCategory,jdbcType=TINYINT}");
        }
        
        if (record.getProductType() != null) {
            sql.VALUES("product_type", "#{productType,jdbcType=TINYINT}");
        }
        
        if (record.getStatus() != null) {
            sql.VALUES("status", "#{status,jdbcType=TINYINT}");
        }
        
        if (record.getBalance() != null) {
            sql.VALUES("balance", "#{balance,jdbcType=DECIMAL}");
        }
        
        if (record.getScene() != null) {
            sql.VALUES("scene", "#{scene,jdbcType=VARCHAR}");
        }
        
        if (record.getTip() != null) {
            sql.VALUES("tip", "#{tip,jdbcType=VARCHAR}");
        }
        
        return sql.toString();
    }

    public String updateByPrimaryKeySelective(Bill record) {
        SQL sql = new SQL();
        sql.UPDATE("bill");
        
        if (record.getUserId() != null) {
            sql.SET("user_id = #{userId,jdbcType=VARCHAR}");
        }
        
        if (record.getTradeId() != null) {
            sql.SET("trade_id = #{tradeId,jdbcType=VARCHAR}");
        }
        
        if (record.getTradeTime() != null) {
            sql.SET("trade_time = #{tradeTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getTotalFee() != null) {
            sql.SET("total_fee = #{totalFee,jdbcType=DECIMAL}");
        }
        
        if (record.getIsIncome() != null) {
            sql.SET("is_income = #{isIncome,jdbcType=TINYINT}");
        }
        
        if (record.getOtherSide() != null) {
            sql.SET("other_side = #{otherSide,jdbcType=VARCHAR}");
        }
        
        if (record.getRefund() != null) {
            sql.SET("refund = #{refund,jdbcType=VARCHAR}");
        }
        
        if (record.getTradeType() != null) {
            sql.SET("trade_type = #{tradeType,jdbcType=TINYINT}");
        }
        
        if (record.getProductId() != null) {
            sql.SET("product_id = #{productId,jdbcType=VARCHAR}");
        }
        
        if (record.getProductCategory() != null) {
            sql.SET("product_category = #{productCategory,jdbcType=TINYINT}");
        }
        
        if (record.getProductType() != null) {
            sql.SET("product_type = #{productType,jdbcType=TINYINT}");
        }
        
        if (record.getStatus() != null) {
            sql.SET("status = #{status,jdbcType=TINYINT}");
        }
        
        if (record.getBalance() != null) {
            sql.SET("balance = #{balance,jdbcType=DECIMAL}");
        }
        
        if (record.getScene() != null) {
            sql.SET("scene = #{scene,jdbcType=VARCHAR}");
        }
        
        if (record.getTip() != null) {
            sql.SET("tip = #{tip,jdbcType=VARCHAR}");
        }
        
        sql.WHERE("id = #{id,jdbcType=VARCHAR}");
        
        return sql.toString();
    }

    /**
     * 复合条件选择 账单详情列表
     * @param record 筛选条件
     * @return sql语句
     */
    public String findSelective(BillQuery record) {
        return sql(record, "id, user_id, trade_id, trade_time, total_fee, is_income, other_side, refund, " +
                "trade_type, product_id, product_category, product_type, status, balance, scene, " +
                "tip");
    }

    /**
     * 复合条件选择 账单详情列表
     * @param record 筛选条件
     * @return sql语句
     */
    public String countSelective(BillQuery record) {
        return sql(record, "sum(total_fee)");
    }

    private String sql(BillQuery record, String s) {
        SQL sql = new SQL();
        sql.SELECT_DISTINCT(s)
                .FROM("bill");

        List<String> list = new ArrayList<>();
        list.add("1=1");
        // 用户id
        if (StringUtils.hasText(record.getUserId())) {
            list.add("user_id = #{userId, jdbcType=VARCHAR}");
        }

        // 账单流水id
        if (StringUtils.hasText(record.getTradeId())) {
            list.add("trade_id=#{tradeId,jdbcType=VARCHAR}");
        }

        // 账单选择的时间区间
        if (record.getBeginTime() != null && record.getEndTime() != null) {
            list.add("trade_time between #{beginTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}");
        } else if (record.getBeginTime() != null) {
            list.add("trade_time >= #{beginTime,jdbcType=TIMESTAMP}");
        } else if (record.getEndTime() != null) {
            list.add("trade_time <= #{endTime,jdbcType=TIMESTAMP}");
        }

        // 收入还是支出
        if (record.getIsIncome() != null) {
            list.add("is_income=#{isIncome,jdbcType=TINYINT}");
        }

        // 支付类型 余额 微信
        if (record.getTradeType() != null) {
            list.add("trade_type=#{tradeType,jdbcType=TINYINT}");
        }

        // 产品类型 打赏 其他
        if (record.getProductType() != null) {
            list.add("product_type=#{productType,jdbcType=TINYINT}");
        }

        // 产品分类 直播 图文 专辑
        if (record.getProductType() != null) {
            list.add("product_type=#{productType,jdbcType=TINYINT}");
        }

        // 交易状态 待入账
        if (record.getStatus() != null) {
            list.add("status=#{status, jdbcType=INTEGER}");
        }

        String[] conditions = new String[1];
        conditions = list.toArray(conditions);
        sql.WHERE(conditions);
        return sql.toString();
    }
}